package com.core.web.initial;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import com.core.cache.Common;
import com.core.cache.CommonCache;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


/**
 * 
/**
 *解析SelectFromXML.xml
 * @author ring
 * 加载MDGLConfig.xml配置文件中的值，赋给Config的变量。 
 * <?xml version="1.0" encoding="GBK"?>
 * <SelectDefineByXML> 
 * <select name="xb"> 
 * <item value="1" option="男"/> 
 * <item * value="0" option="女"/> 
 * </select> <select name="lb"> 
 * <item value="1" * option="部门"/> 
 * <item value="2" option="岗位"/> 
 * </select> 
 * </SelectDefineByXML> *
 */
public class SelectFromXMLLoad  {

    public SelectFromXMLLoad() {
    }

    // 在元属开始事件中进行的工作
    public void parserXML(String fileName) {
        try {
            // 从类路径读取配置文件。
            System.out.println("---加载SelectFromXMLLoad---"+fileName);
           InputStream stream = this.getClass().getResourceAsStream(fileName);
            SAXReader reader = new SAXReader();
            Document doc = reader.read(stream);
            Element root = doc.getRootElement();

            // 解析select集合
            Element select;
            for (Iterator i = root.elementIterator("select"); i.hasNext();) {
                select = (Element) i.next();
                Attribute aName = (Attribute) select.attribute("name");
                String stlectName = aName.getValue();
                System.out.println("---加载---"+stlectName);
                Element item;
                ArrayList list = new ArrayList();
                for (Iterator k = select.elementIterator("item"); k.hasNext();) {
                    item = (Element) k.next();
                    Attribute aValue = (Attribute) item.attribute("value");
                    Attribute aOption = (Attribute) item.attribute("option");
                    list.add(new Common(aValue.getValue(), aOption.getValue()));
                }
                // 添加入缓存
                CommonCache.getInstance().addCommon(stlectName, list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* main method for test: */
    public static void main(String[] args) {
        SelectFromXMLLoad u = new SelectFromXMLLoad();
        u.parserXML("E:\\selectfromxml.xml");
    }
}

